package com.gerfmarquez.nymph.ui.seekbar;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;

import com.gerfmarquez.nymph.R;
import com.gerfmarquez.nymph.ui.listeners.SeekBarOnTouchListener;

public class SpawnNoteView {
	
	public static LinearLayout spawnNoteView( Context context) {
		LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
		SeekBarOnTouchListener seekBarOnTouchListener = new SeekBarOnTouchListener(context);
		
		LinearLayout musicNoteLinearVerticalLayout = ViewInflater.getMusicNoteLinearVerticalLayout(context);
        musicNoteLinearVerticalLayout.setLayoutParams(layoutParams);
        VerticalSeekBar vSeekBar = (VerticalSeekBar)musicNoteLinearVerticalLayout.findViewById(R.id.noteLevel);
        vSeekBar.setManualProgress(15);//Needs Two invocations to do somekind of internal refresh.
        vSeekBar.setManualProgress(16);
        vSeekBar.setOnTouchListener(seekBarOnTouchListener);
        return musicNoteLinearVerticalLayout;
	}
	public static LinearLayout spawnNoteView(final Context context, int note, final int noteLength, Handler uiHandler) {
		LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
		SeekBarOnTouchListener seekBarOnTouchListener = new SeekBarOnTouchListener(context);
		
		LinearLayout musicNoteLinearVerticalLayout = ViewInflater.getMusicNoteLinearVerticalLayout(context);
		musicNoteLinearVerticalLayout.setLayoutParams(layoutParams);
		
		int progress = note * 8;
		
		VerticalSeekBar vSeekBar = (VerticalSeekBar)musicNoteLinearVerticalLayout.findViewById(R.id.noteLevel);
		vSeekBar.setManualProgress(progress-1);//Needs Two invocations to do somekind of internal refresh.
		vSeekBar.setManualProgress(progress);
		vSeekBar.setOnTouchListener(seekBarOnTouchListener);
		
		final ImageButtonNew button = (ImageButtonNew)musicNoteLinearVerticalLayout.findViewById(R.id.noteRhythm);
		uiHandler.post(new Runnable() {
			public void run() {
				button.bringToFront();
				button.setNoteLength(""+noteLength);
		    	if(button.getNoteLength().equals("1")) {
		    		button.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.length_whole));
		    	}
		    	else if(button.getNoteLength().equals("2")) {
		    		button.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.length_half));
		    	}
		    	else if(button.getNoteLength().equals("4")) {
		    		button.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.length_quarter));
		    	}
		    	else if(button.getNoteLength().equals("8")) {
		    		button.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.length_eighth));
		    		
		    	}
		    	else if(button.getNoteLength().equals("3")) {
		    		button.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.length_third));
		    	}
			}
		});
		
		return musicNoteLinearVerticalLayout;
	}

}
